package com.example.administrator.newfragmentation.fragment.simple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.fragmentMain.BaseBlankFragment;
import com.example.administrator.newfragmentation.fragmentMain.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CycleFragment extends BaseFragment {

    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.btn_next_with_finish)
    Button btnNextWithFinish;
    @Bind(R.id.btn_next)
    Button btnNext;
    private int index;
    private static final String ARG_NUMBER = "arg_number";

    public static CycleFragment newInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_NUMBER, index);

        CycleFragment fragment = new CycleFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt(ARG_NUMBER);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cycle, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String title = "循环Fragment" + index;
        tvName.setText(title);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.btn_next_with_finish, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next_with_finish:
                // 启动目标Fragment，并关闭当前Fragment；不要尝试pop()+start()，动画会有问题
                startWithPop(CycleFragment.newInstance(index+1));
                break;
            case R.id.btn_next:
                start(CycleFragment.newInstance(index+1));
                break;
        }
    }
}
